<?php
class UserAction extends Action {

    protected $usermodel = null;
    
    public function __construct() {
        parent::__construct();
        if (is_null($usermodel)) {
            $this->usermodel = D('User'); 
        }
    }

    public function login(){
        if (isLogin()) {
            $this->redirect('Index/index');
        } else {
            if (isset($_POST['username'])) {
                $errmsg = '';
                $status = false;
                $username = trim($_POST['username']);
                $password = $_POST['password'];

                if (isset($_POST['remember']) && $_POST['remember'] == true) {
                    $remember = true;
                }

                $method = '';
                // TODO split login method
                if (preg_match('/@/', $username, $matches)) {
                    $method = 'email';
                } elseif (substr($username, 0, 1) == '1' && strlen($username) == 11) {
                    $method = 'phone'; 
                } else {
                    $method = 'username';
                }

                switch ($method) {
                    case 'username':
                        $user['username'] = $username;
                        $user['password'] = $password;
                        break;

                    case 'phone':
                        $user['phone'] = $username;
                        $user['password'] = $password;
                        break;

                    case 'email':
                        $user['email'] = $username;
                        $user['password'] = $password;
                        break;
                }

                $result = $this->usermodel->login($user);
                
                if ($result['status']) {
                    // login ok
                    $uid = $result['errmsg']['uid'];
                    $userdata = $this->usermodel->getUserById($uid);
                    $_SESSION['logined'] = true;
                    $_SESSION['uid'] = $uid;
                    $_SESSION['username'] = $userdata['username'];
                    $_SESSION['email'] = $userdata['email'];
                    $_SESSION['phone'] = $userdata['phone'];
                    $_SESSION['salt'] = $userdata['salt'];
                    $_SESSION['is_root'] = $userdata['is_root'];
                    $_SESSION['is_baned'] = $userdata['is_baned'];
                    $_SESSION['is_actived'] = $userdata['is_actived'];
                    $_SESSION['token'] = $userdata['token'];
                    $_SESSION['expire_date'] = $userdata['expire_date'];

                    $updateUser['last_login_ip'] = getClientIp();
                    $updateUser['last_login_date'] = null;
                    if ($remember) { $updateUser['expire_date'] = date('Y-m-d H:i:s', time() + 60*60*24*30); }
                    $userUpdate = M('users');
                    $userUpdate->data($updateUser)->where("uid = $uid")->save();
                    $this->redirect('Index/index');
                } else {
                    // login faild
                    $this->assign('errmsg', $result['errmsg']);
                    $this->display();
                }
            } else {
                $this->display();
            }
        }
    }

    public function register(){
        if (isset($_POST['username'])) {
            $username = $_POST['username'];
            $email = $_POST['email'];
            $phone = $_POST['phone'];
            $password = $_POST['password'];
            $passwordagain = $_POST['passwordagain'];

            $user['username'] = $username;
            $user['email'] = $email;
            $user['phone'] = $phone;
            $user['password'] = $password;
            $user['passwordagain'] = $passwordagain;

            $result = $this->usermodel->register($user);
            
            if ($result['status']) {
                $this->assign('successinfo', $result['errmsg']);
                $this->redirect('User/login');
            } else {
                $this->assign('errmsg', $result['errmsg']);
                $this->display();
            }
        } else {
            $this->display();
        }
    }
}
